﻿using System;
using System.Data.Entity;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace ESN.Models
{
    public class Category
    {
        [Key]
        public int CatId { get; set; }

        public int? ParentId { get; set; }

        public string CatName { get; set; }

        public bool CatPublic { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime CatTime { get; set; }

        public Guid UserId { get; set; }
    }

    public class Exercise
    {
        [Key]
        public int ExId { get; set; }

        public int CatId { get; set; }

        public int ExNumQues { get; set; }

        public int? ExDone { get; set; }

        public int? ExShot { get; set; }

        public int? ExAverageTime { get; set; }

        public float? ExAveragePoint { get; set; }

        public int? ExTime { get; set; }

        public string ExName { get; set; }

        public string ExContent { get; set; }

        public string ExCorrect { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime? ExCreateTime { get; set; }

        //[DataType(DataType.DateTime)]
        //[CustomValidation(typeof(QuizCustomValidation), "ValidBeginTime", ErrorMessage = "AA")]
        //public DateTime? ExBeginTime { get; set; }

        //[DataType(DataType.DateTime)]
        //[CustomValidation(typeof(QuizCustomValidation), "ValidEndTime", ErrorMessage = "AA")]
        //public DateTime? ExEndTime { get; set; }

        public bool ExPublic { get; set; }

        public bool ExFreeShot { get; set; }

        public bool ExTrace { get; set; }

        public Guid UserId { get; set; }

        public Guid? EditorId { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime? ExEditTime { get; set; }

        public virtual Category Categories { get; set; }

    }

    public class UserResult
    {
        [Key]
        public int ReId { get; set; }

        public int ExId { get; set; }

        public float RePoint1 { get; set; }

        public float RePoint2 { get; set; }

        public string ReContent { get; set; }

        public bool ReWatting { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime ReCreateTime { get; set; }

        public int TimeSpend { get; set; }

        public Guid UserId { get; set; }

        public virtual Exercise Exercises { set; get; }
    }

    public class ESNQuizDbContext : DbContext
    {
        public DbSet<Exercise> Exercises { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<UserResult> UserResults { get; set; }
    }

    //public class QuizCustomValidation
    //{
    //    public static string errorMessage { get; set; }

    //    public static ValidationResult ValidBeginTime(DateTime? beginTime, ValidationContext context)
    //    {
    //        var currentEx = (Exercise)context.ObjectInstance;
    //        var createTime = currentEx.ExCreateTime;

    //        if (!beginTime.HasValue)
    //            return ValidationResult.Success;

    //        if (DateTime.Compare(createTime.AddMinutes(10), beginTime.Value) >= 0)
    //        {
    //            return new ValidationResult(errorMessage, new List<string> { "ExBeginTime" });
    //        }

    //        return ValidationResult.Success;
    //    }

    //    public static ValidationResult ValidEndTime(DateTime? endTime, ValidationContext context)
    //    {
    //        var currentEx = (Exercise)context.ObjectInstance;
    //        var beginTime = currentEx.ExBeginTime;

    //        if (!endTime.HasValue)
    //            return ValidationResult.Success;

    //        if (beginTime.HasValue)
    //        {
    //            if (DateTime.Compare(beginTime.Value, endTime.Value) >= 0)
    //            {
    //                return new ValidationResult(errorMessage, new List<string> { "ExEndTime" });
    //            }
    //        }

    //        return ValidationResult.Success;
    //    }
    //}

    public class Question
    {
        public int type { get; set; }
        public string ques { get; set; }
        public int point { get; set; }
        public int numAns { get; set; }
        public int max { get; set; }
        public int min { get; set; }
        public List<string> ans { get; set; }
        public List<string> ansA { get; set; }
        public List<string> ansB { get; set; }
    }

    [Serializable]
    public class AnsList
    {
        public int ExId { get; set; }
        public int TimeSpend { get; set; }
        public List<List<string>> Ans { get; set; }
    }

    public class Results
    {
        public List<Question> questions { get; set; }
        public List<List<string>> answers { get; set; }
        public List<List<string>> corrects { get; set; }
    }
}